package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.*;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //处理查询所有部门请求
    @RequiredPermission(name = "用户页面", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        return "employee/list";//找视图,/WEB-INF/views/employee/list.jsp
    }

    //处理删除请求
    @RequiredPermission(name = "用户删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";//让浏览器重新发一次请求
    }

    //去新增或去修改页面
    @RequiredPermission(name = "用户新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        List<Department> departments = departmentService.listAll();
        //查询所有数据往模型中存入,提供数据,让员工新增修改时显示出来让用户选择
        model.addAttribute("departments", departments);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";//让浏览器再发一次请求
    }

    @RequiredPermission(name = "用户新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() == null) {
            employeeService.save(employee, ids);//新增
        } else {
            employeeService.update(employee, ids);//修改
        }
        return new JsonResult();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String username) {
        Map map = new HashMap();
        Employee employee = employeeService.queryByUsername(username);
        map.put("valid", employee == null);
        return map;
    }

    @RequestMapping("/exportXls")
    @RequiredPermission(name = "用户导出", expression = "employee:exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();


        workbook.write(response.getOutputStream());
    }

    //  @RequirePermission(name = "用户导入", expression = "employee:exportXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }

    @RequestMapping("/updatePassword")
    public String updatePassword() {
        return "/common/updatePassword";
    }

    //    @ResponseBody
//    @RequestMapping("/updatePwd")
//    public JsonResult updatePwd(Long id, String oldPassword, String newPassword) {
//        try {
//            Employee employee = employeeService.queryPasswordById(id, oldPassword);
//            employeeService.updatePassword(id, newPassword);
//            return new JsonResult(true, "修改成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new JsonResult(false, e.getMessage());
//        }
//    }
    @ResponseBody
    @RequestMapping("/updatePwd")
    public JsonResult updatePwd(String oldPassword, String newPassword) {
        employeeService.updatePassword2(oldPassword, newPassword);
        return new JsonResult();
    }

    @RequestMapping("/updatePawd")
    @ResponseBody
    public JsonResult updatePawd(String oldPassword, String newPassword) {
        try {
            employeeService.queryPassword(oldPassword);
            employeeService.updatePassword(UserContext.getCurrentUser().getId(), newPassword);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, e.getMessage());
        }
    }

    @RequiredPermission(name = "用户重置密码", expression = "employee:resetPassword")
    @RequestMapping("/resetPassword")
    public String resetPassword(Long id) {
        employeeService.updatePasswordById(id);
        return "redirect:/employee/list";
    }


    @RequiredPermission(name = "用户禁用/恢复", expression = "employee:disableOrRecovery")
    @RequestMapping("/disableOrRecovery")
    public String disableOrRecovery(Long id) {
        employeeService.EmpDisableOrRecovery(id);
        return "redirect:/employee/list";
    }
}
